package trb.complexshield;

import no.hrp.hvrc.rad.calc.intersect.Segment;

public class ComplexShieldOutput implements ComplexShield.Output {
    private static final int INITIAL_LENGTH = 16;
    public float[] data = new float[INITIAL_LENGTH];
    public int size = 0;

    public void clear() {
        size = 0;
    }

    public void ensureCapacity(int minSize) {
        if (data.length < minSize) {
            int newSize = Math.max(data.length * 2, minSize);
            float[] newData = new float[newSize];
            System.arraycopy(data, 0, newData, 0, data.length);
            data = newData;
        }
    }

    @Override
    public void addIntersection(float start, float end) {
        ensureCapacity(size + 2);
        data[size++] = start;
        data[size++] = end;
    }

    private float getTotal() {
        float total = 0f;
        for (int i=0; i<size; i+=2) {
            total += data[i+1] - data[i];
        }
        return total;
    }

    public float getTotal(Segment segment) {
        return getTotal() * segment.length();
    }


}
